
Partial Class Admin_DatabaseDocumentation
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LoadTables()
        LoadProcedures()
        LoadFunctions()
        LoadViews()
        LoadTriggers()
    End Sub

    Private Sub LoadTables()
        Me.TablesDataSource.ConnectionString = EntityController.AppConnectString
        Me.TablesDataSource.SelectCommand = "Select * from DataBaseDocumentationDescriptions where ObjectType='U' Order by ObjectName"
        Me.TableListRepeater.DataSource = Me.TablesDataSource.Select(System.Web.UI.DataSourceSelectArguments.Empty)
        Me.TableListRepeater.DataBind()
    End Sub

    Private Sub LoadProcedures()
        Me.ProcedureDataSource.ConnectionString = EntityController.AppConnectString
        Me.ProcedureDataSource.SelectCommand = "Select * from DataBaseDocumentationDescriptions where ObjectType='P' Order by ObjectName"
        Me.ProcedureRepeater.DataSource = Me.ProcedureDataSource.Select(System.Web.UI.DataSourceSelectArguments.Empty)
        Me.ProcedureRepeater.DataBind()
    End Sub

    Private Sub LoadFunctions()
        Me.FunctionDataSource.ConnectionString = EntityController.AppConnectString
        Me.FunctionDataSource.SelectCommand = "Select * from DataBaseDocumentationDescriptions where ObjectType in ('TF','FN') Order by ObjectName"
        Me.FunctionRepeater.DataSource = Me.FunctionDataSource.Select(System.Web.UI.DataSourceSelectArguments.Empty)
        Me.FunctionRepeater.DataBind()
    End Sub

    Private Sub LoadTriggers()
        Me.TriggerDataSource.ConnectionString = EntityController.AppConnectString
        Me.TriggerDataSource.SelectCommand = "select so.name TriggerName, pt.name TableName, dd.Description " + _
                        "from sysobjects so, sysobjects pt, DatabaseDocumentationDescriptions dd " + _
                        "where(pt.id = so.parent_obj) and so.name=dd.ObjectName and so.xtype='TR' order by so.name"

        Me.TriggerGridView.DataSource = Me.TriggerDataSource.Select(System.Web.UI.DataSourceSelectArguments.Empty)
        Me.TriggerGridView.DataBind()

    End Sub

    Private Sub LoadViews()
        Me.ViewDataSource1.ConnectionString = EntityController.AppConnectString
        Me.ViewDataSource1.SelectCommand = "Select * from DataBaseDocumentationDescriptions where ObjectType='V' Order by ObjectName"
        Me.ViewsRepeater.DataSource = Me.ViewDataSource1.Select(System.Web.UI.DataSourceSelectArguments.Empty)
        Me.ViewsRepeater.DataBind()
    End Sub

    Protected Sub TableListRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles TableListRepeater.ItemDataBound
        Dim grd As GridView = CType(e.Item.FindControl("TableColumnsGridView"), GridView)
        If Not grd Is Nothing Then
            Dim tds As New SqlDataSource
            Dim tableName As String = e.Item.DataItem(0)
            tds.ConnectionString = EntityController.AppConnectString
            tds.SelectCommand = "select * from vwTableStructure where [Table Name]='" & tableName & "' order by [Column Order]"
            grd.DataSource = tds.Select(System.Web.UI.DataSourceSelectArguments.Empty)
            grd.DataBind()
        End If
    End Sub

    Protected Sub ProcedureRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles ProcedureRepeater.ItemDataBound
        BindColumnsGrid("ProcedureParamsGridView", e)
      
    End Sub

    Private Sub BindColumnsGrid(ByVal gridViewName As String, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
        Dim ParamSQL As String = "select so.name as ProcedureName, sc.name as ParameterName, " + _
                      "tp.Name as ParameterType," + _
                      "CASE WHEN tp.Name in ('char','varchar') then '(' + convert(varchar,sc.length) + ')' ELSE '' END as ParameterLength " + _
                      "from sysobjects so, syscolumns sc, systypes tp " + _
                      "where so.id=sc.id and sc.xtype=tp.xtype " + _
                      " and so.name='{0}' order by sc.colorder"


        Dim grd As GridView = CType(e.Item.FindControl(gridViewName), GridView)
        If Not grd Is Nothing Then
            Dim tds As New SqlDataSource
            Dim objectName As String = e.Item.DataItem(0)
            tds.ConnectionString = EntityController.AppConnectString
            tds.SelectCommand = String.Format(ParamSQL, objectName)
            grd.DataSource = tds.Select(System.Web.UI.DataSourceSelectArguments.Empty)
            grd.DataBind()
        End If
    End Sub

    Protected Sub FunctionRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles FunctionRepeater.ItemDataBound
        BindColumnsGrid("FunctionParamsGridView", e)
    End Sub

    Protected Sub ViewsRepeater_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles ViewsRepeater.ItemDataBound
        BindColumnsGrid("ViewColumnsGridView", e)
    End Sub
End Class
